import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { assetLampBaseState } from './typings'
import {
  getAssetLampBaseList,
  getAssetLampBaseInfo,
  addAssetLampBaseInfo,
  updateAssetLampBaseInfo,
  checkDeleteAssetLampBaseInfo,
  deleteAssetLampBaseInfo,
  exportAssetLampBaseInfo,
  updateAssetLampBaseMapLocation
  // getLampBaseTypeSelectList
} from '~/api/assetManagement/facilityManagement/assetLampBase'
// import { getNodeByParentCode } from '~/api/assetManagement/facilityManagement/powerDistributionFacilities'
import { getTenantDictDataDictionaryByCode } from '~/api/global'
import { filterValueEnum, DownloadAttachment } from '~/utils/util'

const assetLampBase: Module<assetLampBaseState, assetLampBaseState> = {
  namespaced: true,
  state: {
    lampBaseTypeSelectList: [],
    lampBaseTypeEnum: {}
  },
  getters: {
    lampBaseTypeSelectList: (state) => state.lampBaseTypeSelectList,
    lampBaseTypeEnum: (state) => state.lampBaseTypeEnum
  },
  mutations: {
    SET_FACILITY_TYPE_SELECT_LIST: (state, payload) => {
      state.lampBaseTypeSelectList = payload
      state.lampBaseTypeEnum = filterValueEnum(payload, 'id', 'dictValue')
    }
  },
  actions: {
    // 获取灯具主列表
    async GetAssetLampBaseList<S, T>(_: ActionContext<S, T>, params: T) {
      return await getAssetLampBaseList<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 导出灯具
    async ExportAssetLampBaseInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await exportAssetLampBaseInfo<T>(params).then((res: AxiosResponse) => {
        const contentDisposition = res?.headers?.['content-disposition']
        if (contentDisposition && res.data) {
          DownloadAttachment(decodeURI(contentDisposition.substring(20)), res.data)
        }
        return res.data
      })
    },

    // 新增灯具 基础资料
    async AddAssetLampBaseInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await addAssetLampBaseInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 编辑灯具 基础资料 原内容
    async GetAssetLampBaseInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await getAssetLampBaseInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 编辑灯具 基础资料 提交
    async UpdateAssetLampBaseInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await updateAssetLampBaseInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 编辑灯具 地图位置
    async UpdateAssetLampBaseMapLocation<S, T>(_: ActionContext<S, T>, params: T) {
      return await updateAssetLampBaseMapLocation<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 校验灯具是否可删除
    async CheckDeleteAssetLampBaseInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await checkDeleteAssetLampBaseInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 删除灯具
    async DeleteAssetLampBaseInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await deleteAssetLampBaseInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },

    // 灯杆类型 下拉列表
    async GetNodeByParentCode<S, T>({ commit }: ActionContext<S, T>) {
      return await getTenantDictDataDictionaryByCode({ code: 'lamp_base_cate' }).then(
        (res: AxiosResponse) => {
          commit('SET_FACILITY_TYPE_SELECT_LIST', res?.data?.data || [])
          return res?.data
        }
      )
    }
  }
}

export default assetLampBase
